--
-- Author: yawei.zhu
-- Date: 2018-06-05 18:19:58
--
-- PvpChampionController
--

local PvpChampionView = import("..views.PvpChampionView")
local PvpChampionTeamView = import("..views.PvpChampionTeamView")
local PvpChampionBookView = import("..views.PvpChampionBookView")
local PvpChampionOtherView = import("..views.PvpChampionOtherView")
local PvpChampionNoticeView = import("..views.PvpChampionNoticeView")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ControllerBase)

function ClassRef:ctor(data)
    self.super.ctor(self, data)
end

function ClassRef:init()
    -- self._model = app:getInst("PvpChampionTeamModel")
    self._teamModel = app:getInst("TeamModel")
    self._pvpModel = app:getInst("PvpModel")
end

function ClassRef:removeListeners()
    self.super.removeListeners(self)
end

function ClassRef:listEvent()
    return {
        "onTeamUpdate",
        "onTeamMemberUpdate",
        "onTeamLeaderChanged",
        "onTeamSettingUpdate",
        "onTeamNewApplyer"
    }
end

function ClassRef:onEvent(eventName, data)
    if eventName == "onTeamUpdate" then -- 队伍更新
        self:_refreshTeamMemberList()
    elseif eventName == "onTeamMemberUpdate" or eventName == "onTeamLeaderChanged" then
        self:_refreshTeamMemberList()
    elseif eventName == "onTeamSettingUpdate" then -- 队伍设置更新
        -- donothing
    elseif eventName == "onTeamNewApplyer" then -- 来新申请人
        if not self:isViewAlive() then return end
        if self._teamModel:isMyTeam() then
            self._teamModel:reqGetApplicationList()
        end
        self:_refreshNewApplyerFlag()
    end
end


function ClassRef:openView(data)
    if self._teamModel:isMyTeam() then -- 打开UI的时候，队长拉取一次申请列表
        self._teamModel:reqGetApplicationList()
    end

    if cc.isDead(self._view) then
        self._view = PvpChampionView.new(app.mvc.VIEWTYPE_BLUR)
        self._view:onCloseView(handler(self, self.closeView))
        self._view:onClearView(handler(self, self.clearView))
        self._view:onOperate(handler(self, self.onOperate))
        self:addChild(self._view, data)
    end

    self._view:hide()
    self._pvpModel:reqThroneTeamInfo(function()
        if cc.isAlive(self._view) then
            self._view:show()
            self._view:updateView(data, self._pvpModel:getThroneGame(), self._pvpModel:getThroneTeam())
        end
    end)
end

function ClassRef:closeView(data)
    if cc.isAlive(self._view) then
        self:removeChild(self._view, data)
        -- self:clearView() -- 会自动调用clearView
    end
end
function ClassRef:clearView()
    if self._view then
        self._view = nil
    end
end

-- 打开队伍详细信息界面
function ClassRef:openTeamView(throneTeam)
    local view = PvpChampionTeamView.new(app.mvc.VIEWTYPE_BLUR)
    UIHandler:addSystemUi(view, view:getViewName())
    view:onCloseView(function(sender)
        UIHandler:removeSystemUiByKey(sender:getViewName())
    end)

    view:refresh(throneTeam)
    view:show()
end

-- 打开奖励预览界面
function ClassRef:openBookView(day)
    local view = PvpChampionBookView.new(app.mvc.VIEWTYPE_BLUR)
    UIHandler:addSystemUi(view, view:getViewName())
    view:onCloseView(function(sender)
        UIHandler:removeSystemUiByKey(sender:getViewName())
    end)

    view:show()
    view:updateView(day)
end

-- 打开其他天的16强队伍信息界面
function ClassRef:openOtherDayFormView(throneInfo)
    local view =  PvpChampionOtherView.new(app.mvc.VIEWTYPE_BLUR)
    view:onOperate(handler(self, self.onOperate))
    UIHandler:addSystemUi(view, view:getViewName())
    view:onCloseView(function(sender)
        UIHandler:removeSystemUiByKey(sender:getViewName())
    end)

    view:show()
    view:updateView(throneInfo)
end

-- 未报名时，打开的提示
function ClassRef:openNoticeView(param)
    local view = PvpChampionNoticeView.new()
    view:onOperate(handler(self, self.onOperate))
    UIHandler:addPopupUi(view, view:getViewName())
    view:onCloseView(function(sender)
        UIHandler:removePopupUiByKey(sender:getViewName())
    end)

    view:show()
    view:updateView(param)
end


-- 请求报名或取消报名 回调
function ClassRef:onChangeRegisterStatus()
    if not self:isViewAlive() then return end

    local day = app:getWeekDay()
    local throneInfo = self._pvpModel:getThroneInfo(day)
    self._view:updateView(throneInfo, self._pvpModel:getThroneGame(), self._pvpModel:getThroneTeam())
end

-- 更新报名的队伍信息 服务器下发
function ClassRef:onUpdateThroneTeamInfo()
    if not self:isViewAlive() then return end

    local day = app:getWeekDay()
    local throneInfo = self._pvpModel:getThroneInfo(day)
    self._view:updateView(throneInfo, self._pvpModel:getThroneGame(), self._pvpModel:getThroneTeam())
end

-- 更新排名信息
function ClassRef:updateChampionRank(rankInfo)
    if not self:isViewAlive() then return end
    if not rankInfo then return end
    self._view:updateChampionRank(rankInfo)
end

-- 更新左侧冠军
function ClassRef:updateChampionCharacter(championInfo)
    if not self:isViewAlive() then return end
    local day = app:getWeekDay()
    local throneInfo = championInfo:getThroneInfo(day)
    self._view:updateChampionCharacter(throneInfo)
end

-------------------------------------------------------
function ClassRef:_refreshTeamMemberList()
    if not self:isViewAlive() then return end
    self._view:_refreshTeamMemberList(self._teamModel:getTeam(), self._teamModel:isNewApplyerFlag())
end

function ClassRef:_refreshNewApplyerFlag(visible)
    if not self:isViewAlive() then return end
    self._view:_refreshNewApplyerFlag( self._teamModel:isNewApplyerFlag() )
end


-------------------------------------------------------
function ClassRef:onOperate(op, data)
    print("PvpChampionController:onOperate op = " .. op)

    if op == "PvpChampion_createTeam" then -- 创建队伍
        self._teamModel:reqCreateTeam()
    elseif op == "PvpChampion_quitTeam" then -- 离开队伍
        self._teamModel:reqQuitTeam()
    elseif op == "PvpChampion_showApplyList" then -- 展开申请列表
        if self._teamModel:isMyTeam() then
            app:sendMsg("NewTeamApplyController", "openView", { 2 } ) -- 弹出标签2-申请标签
        else
            display.pushToast( L("champion_notice1") )
        end
    elseif op == "PvpChampion_TeamSettingUI" then -- 设置UI
        app:sendMsg("NewTeamSettingController", "openView")

    elseif op == "MemberInfoPanel_openInvitePanel" then -- 弹出邀请panel
        app:sendMsg("NewTeamApplyController", "openView", { 1 } ) -- 弹出标签1-邀请标签
    elseif op == "PvpChampion_register" then -- 报名
        -- TODO 判断报名人数是否超过3个人
        self._pvpModel:reqRegister(true)
    elseif op == "PvpChampion_CancelSign" then -- 取消报名
        self._pvpModel:reqRegister(false)
    elseif op == "PvpChampion_CancelMatch" then -- 取消匹配
        self._pvpModel:reqRegister(false)
    elseif op == "PvpChampion_EnterMatch" then -- 进入战场
        -- 判断是否过了准备时间
        local throneGame = self._pvpModel:getThroneGame()
        if app.session:getServerTime() < throneGame.prepareTime then
            local timeData = os.date("*t", throneGame.prepareTime)
            display.pushToast( string.format( L("champion_notice2"), timeData.hour, timeData.min) )
        else
            self._pvpModel:reqEnterThroneMap()
        end
    elseif op == "PvpChampion_search_1" then -- search1
        self:openBookView(app:getWeekDay())
    elseif op == "PvpChampion_search_2" then -- search2
        self:openBookView(app:getWeekDay())
    elseif op == "PvpChampion_search_3" then -- search3
        self:openBookView(app:getWeekDay())
    elseif op == "PvpChampion_search_4" then -- search4
        local day = data
        if not day then 
            day = app:getWeekDay()
        end
        self:openBookView(day)
    elseif op == "refreshTeamMemberList" then
        self:_refreshTeamMemberList()
    elseif op == "reqThroneRankInfo" then
        self._pvpModel:reqThroneRankInfo()
    elseif op == "reqFinalTeamInfo" then
        self._pvpModel:reqThroneFinalTeamInfo(data) 
    elseif op == "reqThroneTeamById" then
        self._pvpModel:reqThroneTeamById(data.teamId, data.cb)       
    elseif op == "showTeamDetailInfo" then
        dump(data)
        self._pvpModel:reqThroneTeamById(data, function(throneTeam)
            if not throneTeam then return end
            print("aaaaaaaaaaaa")
            -- 显示队伍详细信息
            self:openTeamView(throneTeam)
        end)
    elseif op == "reqThroneKnockOutTeam" then
        self._pvpModel:reqThroneKnockOutTeam(data[1], data[2])
    elseif op == "openTeamView" then
        self:openTeamView(data)
    elseif op == "openChampionView" then
        self._pvpModel._pvpChampionInfo = nil -- 把冠军赛信息置空，等跳转到了准备场会再把所有的信息拉取一遍
        app:sendMsg("PvpController", "openView", 2)
    elseif op == "openChampionTeamView" then
        self._pvpModel:reqChampionSeasonInfo(function()
            local throneInfo = self._pvpModel:getThroneInfo()
            self:openView(throneInfo)
        end)
    end

        
end


                    



return ClassRef
